package month1;

import java.util.*;

public class DailyTemperatures739_08 {
    public static void main(String[] args) {
//        System.out.println(Arrays.toString(dailyTemperatures(new int[]{73, 74, 75, 71, 69, 72, 76, 73})));
//        StringBuilder sb = new StringBuilder();
        System.out.println(Solution.groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"}));
    }

    /**
     * 单调栈
     * @param temperatures
     * @return
     */
    static int[] dailyTemperatures(int[] temperatures) {
        Deque<Integer> stack = new LinkedList<>();
        int[] ret = new int[temperatures.length];
        for (int i = 0; i < temperatures.length; i++) {
            while (!stack.isEmpty() && temperatures[stack.peek()] < temperatures[i]) {
                int pre = stack.pop();
                ret[pre] = i - pre;
            }
            stack.push(i);
        }
        // int float char double short boolean long byte
        return ret;
    }

    static class Solution {
        public static  List<List<String>> groupAnagrams(String[] strs) {
            Map<String, List<String>> map = new HashMap<>();
            for(String str: strs) {
                char[] chars = str.toCharArray();
                Arrays.sort(chars);
                String s = new String(chars);
                List<String> tmp = map.get(s);
                if (tmp == null) {
                    tmp = new ArrayList<>();
                }
                tmp.add(str);
                map.put(s, tmp);
            }
            return new ArrayList(map.values());
        }
    }

    public int[] topKFrequent(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        for(int num: nums) map.put(num, map.getOrDefault(num, 0) + 1);
        Queue<Integer> pq = new PriorityQueue<>((a, b) -> map.get(b) - map.get(a));
        for(int num: nums) {
            pq.offer(num);
            if (pq.size() > k) pq.poll();
        }
        return pq.stream().mapToInt(Integer::intValue).toArray();
    }
}